// pages/detail/detail.js
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: '',
    deviceID: '',
    mac: '',
    show: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  nextPage: function () {
    wx.navigateTo({
      url: '../webView/webView'
    })
  },
  // 断开与蓝牙低功耗设备的连接。
  closeBLEConnection() {
    // this.writeUnbound()
    wx.closeBLEConnection({
      deviceId: wx.getStorageSync('deviceId'),
    })
    // wx.closeBluetoothAdapter()
    let list = ''
    let show = ''
    let mac = ''
    let name = ''
    let deviceID = ''
    this.setData({
      list, deviceID, mac, name, show
    })
    wx.removeStorageSync('blueToothName')
    wx.removeStorageSync('blueToothType')
    wx.removeStorageSync('blueToothData')
    setTimeout(()=>{
      wx.navigateBack({
        delta:1
      })
    },)

  },
   // 解绑设备
   writeUnbound(hex){
    
    wx.writeBLECharacteristicValue({
      deviceId: wx.getStorageSync('deviceId'),
      serviceId: wx.getStorageSync('serviceId'),
      characteristicId: wx.getStorageSync('characteristicId'),
      value: hex,
      success: res => {
        console.log(res)
      },
      fail: res => {
        console.log(res)
      }
    })
  },
   // 将数据转成16进制
   string2buffer(str) {
    let val = ""
    if (!str) return;
    let length = str.length;
    let index = 0;
    let array = []
    while (index < length) {
      array.push(str.substring(index, index + 2));
      index = index + 2;
    }
    val = array.join(",");
    // 将16进制转化为ArrayBuffer
    return new Uint8Array(val.match(/[\da-f]{2}/gi).map(function (h) {
      return parseInt(h, 16)
    })).buffer
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.onBLECharacteristicValueChange((res) => {
      let str = util.ab2hex(res.value)
      console.log(str)
      if (str.slice(0, 2) == '53') {
        wx.setStorageSync('blueToothData', util.splitBlueStr(str))
        console.log(util.splitBlueStr(str))
        // let list = wx.getStorageSync('blueToothData')
        let list = util.splitBlueStr(str)
        let show = wx.getStorageSync('blueToothType')
        let deviceID = ''
        let mac = wx.getStorageSync('deviceId')
        let name = wx.getStorageSync('blueToothName')
        list.map((item, index) => {
          if (index > 0 && index < 11) {
            console.log(item.substr(item.length - 1, 1))
            if(index == 1 && list[index].toString().length == 1){
              deviceID = '0' + deviceID
            }
            deviceID += item.substr(item.length - 1, 1)
          }
        })
        // blueToothObj.deviceID = deviceID
        console.log(deviceID)
        this.setData({
          list, deviceID, mac, name, show
        })
      }
    })
    this.writeUnbound(this.string2buffer('0x560x01'))
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})